package com.yjjxt

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Hello24Sort {
  def main(args: Array[String]): Unit = {

    //创建SparkContext
    val sparkContext = new SparkContext((new SparkConf().setMaster("local").setAppName("Join" + System.currentTimeMillis())))
    //开始读取数据
    val lines: RDD[String] = sparkContext.textFile("src/main/resources/sort.txt")
    //开始组装对象
    val ssks: RDD[(SecondSortKey, Int)] = lines.map(ele => (new SecondSortKey(ele.split("\\s")(0), ele.split("\\s")(1).toInt), 1))

    //排序对象
    val sort: RDD[(SecondSortKey, Int)] = ssks.sortByKey(true)

    sort.foreach(println)

    sparkContext.stop()
  }
}

/**
 * 可以使用Comparable 和 Ordered 作为特征类
 *
 * @param keys
 * @param keyi
 */
case class SecondSortKey(keys: String, keyi: Int) extends Comparable[SecondSortKey] {
  override def compareTo(that: SecondSortKey): Int = {
    var result: Int = this.keys.compareTo(that.keys)
    if (result == 0) {
      result = this.keyi.compareTo(that.keyi)
    }
    result
  }
}